<?php

class m_sms extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->db_sms = $this->load->database('sms', true);
    }

    function inbox_page($limit, $start) {
        $this->db->limit($limit, $start);
        return $this->db->get('inbox')->result();
    }

    function jumlahbaris_i() {
        return $this->db->count_all('inbox');
    }

    function jumlahbaris_o() {
        return $this->db->count_all('sentitems');
    }

    function outbox_page($limit, $start) {
        $this->db->limit($limit, $start);
        return $this->db->get('sentitems')->result();
    }

    function inbox() {
        $sql = "SELECT * FROM inbox";
        $q = $this->db_sms->query($sql);
        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    function write() {
        foreach ($this->input->post('DestinationNumber') as $no) {
            $simpan_data[] = array(
                'DestinationNumber' => $no,
                'TextDecoded' => $this->input->post('TextDecoded'),
                'CreatorID' => $this->input->post('CreatorID')
            );
        }
        $simpan = $this->db_sms->insert_batch('outbox', $simpan_data);
        return $simpan;
    }

    function broadcast() {
        $simpan_data = array(
            'DestinationNumber' => $this->input->post('DestinationNumber'),
            'TextDecoded' => $this->input->post('TextDecoded'),
            'CreatorID' => $this->input->post('CreatorID')
        );
        $simpan = $this->db_sms->insert('outbox', $simpan_data);
        return $simpan;
    }

    function sent() {
        $sql = "SELECT * FROM sentitems";
        $q = $this->db_sms->query($sql);
        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    function balas($no) {
        $q = "SELECT * FROM user WHERE no = '$no'";
        $query = $this->db->query($q);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    function reply() {
        $simpan_data = array(
            'DestinationNumber' => $this->input->post('DestinationNumber'),
            'TextDecoded' => $this->input->post('TextDecoded'),
            'CreatorID' => $this->input->post('CreatorID')
        );
        $simpan = $this->db_sms->insert('outbox', $simpan_data);
        return $simpan;
    }

}